package com.intigas.credigas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import com.intigas.credigas.model.Credito;

public class MySQLCreditoDAO implements EntityDAO<Credito> {

	EntityManager em;
	
	public MySQLCreditoDAO(EntityManager em) {
		this.em = em;
	}
	
	@Override
	public Credito insert(Credito t) {
		em.persist(t);
		return t;
	}

	@Override
	public void update(Credito t) {
		em.merge(t);		
	}

	@Override
	public void delete(Credito t) {
		em.remove(t);		
	}

	@Override
	public Credito findById(int id) {
		return em.find(Credito.class, id);
	}

	@Override
	public Credito findById(Credito t) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Credito> listAll() {
		try{
			Query query = em.createQuery("Select o from Credito o");
			List<Credito> creditos = query.getResultList();
			return creditos;			
		}catch (NoResultException e) {			
			return null;
		}
	}
}
